package com.oss.config.formatter;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * @author jianyang.liu
 * @date 8/7/2023 3:43 PM
 */
@JsonComponent
public class BigDecimalSerializer extends StdSerializer<BigDecimal> implements ContextualSerializer {

    private  String pattern;

    private  JsonFormat.Shape shape;

    private  int newScale;

    private RoundingMode roundingMode;
    /**
     *
     *  是否将百分制数据转化为小数点数据，true：转化为小数点，false：不转化
     */
    private boolean convertPercentToDecimal;

    private  boolean stripTrailingZeros;



    public BigDecimalSerializer(){
        super(BigDecimal.class);
    }

    public BigDecimalSerializer(BigDecimalFormatter annotation){
        super(BigDecimal.class);
        this.pattern = annotation.pattern();
        this.shape = annotation.shape();
        this.newScale = annotation.newScale();
        this.roundingMode = annotation.roundingMode();
        this.stripTrailingZeros = annotation.stripTrailingZeros();
        this.convertPercentToDecimal = annotation.convertPercentToDecimal();
    }


    @Override
    public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (value == null) {
            jsonGenerator.writeNull();
            return;
        }
        // true：百分制转化为小数点，false：不转化
        BigDecimal processedValue = convertPercentToDecimal ? value.divide(new BigDecimal("100")) : value;


        if (shape == JsonFormat.Shape.STRING){
            String output =  new DecimalFormat(pattern).format(processedValue);
            jsonGenerator.writeString(output);
        }else{
            BigDecimal output = processedValue.setScale(newScale, roundingMode);
            if (stripTrailingZeros){
                output= output.stripTrailingZeros();
            }
            jsonGenerator.writeNumber(output.toPlainString());
        }

    }


    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) {
        // 如果没有属性信息，返回默认的数字序列化器
        if (beanProperty==null){
            return NumberSerializer.instance;
        }
        AnnotatedMember member = beanProperty.getMember();
        // 如果有注解，创建新的序列化器实例
        BigDecimalFormatter annotation = member.getAnnotation(BigDecimalFormatter.class);
        if (annotation != null){
            return new BigDecimalSerializer(annotation);
        }
        return NumberSerializer.instance;
    }
}

